//
//  MapDetailViewController.m
//  Hui
//
//  Created by HawkingWang on 16/11/4.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "MapDetailViewController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import "MyAnnotationView.h"

@interface MapDetailViewController () <BMKMapViewDelegate,BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate>

@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

@end

@implementation MapDetailViewController
{
    BMKLocationService *_locationService;
    BMKPointAnnotation *_takeAnnotation;
    BMKPointAnnotation *_deliveryAnnotation;
    MyAnnotationView *_takeAnnoView;
    MyAnnotationView *_deliveryAnnoView;
    BMKGeoCodeSearch *_searcher;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //地图
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
    _mapView.zoomLevel = 15;
    _mapView.delegate = self;
    _mapView.mapType = BMKMapTypeStandard;
    _mapView.overlookEnabled = NO;
    _mapView.rotateEnabled = NO;
    //定位
    _locationService = [[BMKLocationService alloc]init];
    _locationService.delegate = self;
    [_locationService startUserLocationService];
    
    
    //初始化检索对象
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    
    //取货地址
    _takeAnnotation = [[BMKPointAnnotation alloc] init];
    _takeAnnotation.coordinate = _takeCoordinate;
    [_mapView addAnnotation:_takeAnnotation];
    //送货地址
    _deliveryAnnotation = [[BMKPointAnnotation alloc] init];
    _deliveryAnnotation.coordinate = _deliveryCoordinate;
    [_mapView addAnnotation:_deliveryAnnotation];
}

#pragma mark === BaiduSDKDelegate ===
#pragma mark === 处理方向变更信息 ===
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
}
#pragma  mark === 处理位置坐标更新 ===
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    CLLocationCoordinate2D user_coordinate = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    //设置中心点
    //更新数据
    [_mapView updateLocationData:userLocation];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = (user_coordinate.latitude + _deliveryAnnotation.coordinate.latitude + _takeAnnotation.coordinate.latitude) / 3;
    coordinate.longitude = (user_coordinate.longitude + _deliveryAnnotation.coordinate.longitude + _takeAnnotation.coordinate.longitude) / 3;
    _mapView.centerCoordinate = coordinate;
    [_locationService stopUserLocationService];
}
#pragma mark === 反向地理编码检索 ===
- (void)getAnnotationAddressWithCoordinate:(CLLocationCoordinate2D)coordinate{
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
    reverseGeoCodeSearchOption.reverseGeoPoint = coordinate;
    BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
    reverseGeoCodeSearchOption = nil;
    if (flag)
    {
        NSLog(@"反geo检索发送成功");
        
    }
    else
    {
        NSLog(@"反geo检索发送失败");
        [SVProgressHUD dismiss];
    }
}
#pragma mark === 反向地理检索回调 ===
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        //        _address.text = result.address;
        NSLog(@"%@", result.address);
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
    [SVProgressHUD dismiss];
}

#pragma mark === 大头针 ===
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        if (!_takeAnnoView) {
            // 创建大头针
            _takeAnnoView = [MyAnnotationView annotationViewWithMap:mapView];
            // 设置模型
            _takeAnnoView.myImage = [UIImage imageNamed:@"map-q"];
            _takeAnnoView.annotation = _takeAnnotation;
            return _takeAnnoView;
        }else{
            _deliveryAnnoView = [MyAnnotationView annotationViewWithMap:_mapView];
            // 设置模型
            _deliveryAnnoView.myImage = [UIImage imageNamed:@"map-s"];
            _deliveryAnnoView.annotation = _deliveryAnnotation;
            return _deliveryAnnoView;
        }
    }
    return nil;
}

- (IBAction)back:(id)sender {
    [_locationService stopUserLocationService];
    _mapView.showsUserLocation = NO;
    _locationService.delegate = nil;
    _mapView.delegate = nil;
    _searcher.delegate = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
